package stc.skymobi.bean.xip.saip.test;


import org.apache.commons.lang3.ArrayUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import stc.skymobi.bean.bytebean.core.BeanFieldCodec;
import stc.skymobi.bean.util.meta.Int2TypeMetainfo;
import stc.skymobi.bean.xip.util.XipSignalUtils;
import stc.skymobi.transport.protocol.xip.XipHeader;
import stc.skymobi.util.ByteUtils;

public class SaipSignalUtilsTestCase {

	private	BeanFieldCodec		beanCodec;
	private	Int2TypeMetainfo	metainfo;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
    		"stc/skymobi/bean/xip/saip/test/testSaipCodec.xml");

        beanCodec = (BeanFieldCodec)ctx.getBean("beanCodec");
		
        metainfo = (Int2TypeMetainfo)ctx.getBean("typeMetainfo");
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testSimpleSaipSignal() throws Exception {
		DemoSaipReq signal = new DemoSaipReq();
		
		signal.setDstModule((short)100);
		signal.setSrcModule((short)1);
		signal.setName("hello, world");
		
		byte[] bytes = XipSignalUtils.encodeSaipSignal(signal, beanCodec);
		
		System.out.println( 
			ByteUtils.bytesAsHexString(bytes, 1024) );
		
		XipHeader hdr = XipSignalUtils.decodeSaipSignalHeader(bytes, beanCodec);
		
		Assert.assertNotNull(hdr);

		System.out.println( "XipHeader:" + hdr );
		
		byte[] bodyBytes = ArrayUtils.subarray(bytes, XipHeader.SAIP_HEADER_LENGTH, bytes.length);
		
		DemoSaipReq signalDecoded = 
			(DemoSaipReq)XipSignalUtils.decodeSaipSignalBody(
					hdr, bodyBytes, beanCodec, metainfo);
		
		Assert.assertEquals(signalDecoded, signal);
		
	}
	
}
